package cn.yuanqiao.web.controller.archive.appManager;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import cn.yuanqiao.common.utils.SnowflakeIdGenerator;
import cn.yuanqiao.common.utils.SecurityUtils;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import cn.yuanqiao.common.annotation.Log;
import cn.yuanqiao.common.enums.BusinessType;
import cn.yuanqiao.archive.appManager.domain.YqdaTrees;
import cn.yuanqiao.archive.appManager.service.IYqdaTreesService;
import cn.yuanqiao.common.web.controller.BaseController;
import cn.yuanqiao.common.core.domain.AjaxResult;
import cn.yuanqiao.common.utils.poi.ExcelUtil;
import cn.yuanqiao.common.web.page.TableDataInfo;

/**
 * treesController
 * 
 * @author 赵常飞
 * @date 2023-06-17
 */
@RestController
@RequestMapping("/archive/trees")
public class YqdaTreesController extends BaseController
{
    @Autowired
    private IYqdaTreesService yqdaTreesService;

    /**
     * 查询trees列表
     */
    //@PreAuthorize("@ss.hasPermi('archive:trees:list')")
    @GetMapping("/list")
    public TableDataInfo list(@RequestParam("parentId") Long parentId)
    {
        YqdaTrees yqdaTrees=new YqdaTrees();
        yqdaTrees.setParentId(parentId);
        startPage();
        List<YqdaTrees> list = yqdaTreesService.selectYqdaTreesList(yqdaTrees);
        return getDataTable(list);
    }

    /**
     * 导出trees列表
     */
    //@PreAuthorize("@ss.hasPermi('archive:trees:export')")
    @Log(title = "trees", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YqdaTrees yqdaTrees)
    {
        List<YqdaTrees> list = yqdaTreesService.selectYqdaTreesList(yqdaTrees);
        ExcelUtil<YqdaTrees> util = new ExcelUtil<YqdaTrees>(YqdaTrees.class);
        util.exportExcel(response, list, "trees数据");
    }

    /**
     * 获取trees详细信息
     */
    //@PreAuthorize("@ss.hasPermi('archive:trees:query')")
    @GetMapping(value = "/{Id}")
    public AjaxResult getInfo(@PathVariable("Id") Long Id)
    {
        return success(yqdaTreesService.selectYqdaTreesById(Id));
    }

    /**
     * 新增trees
     */
    //@PreAuthorize("@ss.hasPermi('archive:trees:add')")
    @Log(title = "trees", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JSONObject jsonObject)
    {
        return toAjax(yqdaTreesService.insertYqdaTrees(jsonObject));
    }

    /**
     * 修改trees
     */
    //@PreAuthorize("@ss.hasPermi('archive:trees:edit')")
    @Log(title = "trees", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YqdaTrees yqdaTrees)
    {
        return toAjax(yqdaTreesService.updateYqdaTrees(yqdaTrees));
    }

    /**
     * 删除trees
     */
    //@PreAuthorize("@ss.hasPermi('archive:trees:remove')")
    @Log(title = "trees", businessType = BusinessType.DELETE)
	@DeleteMapping("/{Ids}")
    public AjaxResult remove(@PathVariable Long[] Ids)
    {
        return toAjax(yqdaTreesService.deleteYqdaTreesByIds(Ids));
    }
    @Log(title = "根据配置的授权获取档案树", businessType = BusinessType.SELECT)
    @GetMapping("/gettrees/{menuId}")
    public AjaxResult gettrees(@PathVariable Long menuId){
        Boolean isDagly = SecurityUtils.isDagly();
        if(isDagly){
            return success(yqdaTreesService.getTreeList());
        }
        return success(yqdaTreesService.gettrees(menuId));
    }
    @Log(title = "获取完整的档案树", businessType = BusinessType.SELECT)
    @GetMapping("/gettreescomplate")
    public AjaxResult gettreescomplate(){
        return success(yqdaTreesService.getTreeList());
    }
    /**
     * 获取分类字典项
     */
    @PostMapping("/getfl")
    public AjaxResult getfl(@RequestBody JSONObject json){
        return success(yqdaTreesService.getfl(json));
    }
    //获取角色树
    @Log(title = "获取角色树", businessType = BusinessType.SELECT)
    @GetMapping("/getroletree")
    public Map<String,Object> getroletree(){
        return success(yqdaTreesService.getroletree());
    }
    //获取菜单树
    @Log(title = "获取菜单树", businessType = BusinessType.SELECT)
    @GetMapping("/getmunutreebyrole/{roleId}")
    public Map<String,Object> getmunutreebyrole(@PathVariable("roleId") Long roleId){
        return success(yqdaTreesService.getmunutreebyrole(roleId));
    }

    //根据角色,菜单获取档案树
    @Log(title = "根据角色、菜单获取回显树(全部节点)", businessType = BusinessType.SELECT)
    @GetMapping("/gettreebyrolemenuxh/{roleId}/{menuId}")
    public AjaxResult gettreebyrolemenuxh(@PathVariable("roleId") Long roleId,@PathVariable("menuId") Long menuId){
        return success(yqdaTreesService.gettreebyrolemenuxh(roleId,menuId,""));
    }
    @Log(title = "根据角色、菜单获取回显树(只要回显节点)", businessType = BusinessType.SELECT)
    @GetMapping("/gettreebyrolemenuxh2/{roleId}/{menuId}")
    public AjaxResult gettreebyrolemenuxh2(@PathVariable("roleId") Long roleId,@PathVariable("menuId") Long menuId){
        return success(yqdaTreesService.gettreebyrolemenuxh(roleId,menuId," where CHOOESED=1"));
    }
    //添加树授权
    @Log(title = "添加树授权", businessType = BusinessType.SELECT)
    @PostMapping("/addtreeshouquan")
    public AjaxResult addtreeshouquan(@RequestBody JSONObject json){
        return success(yqdaTreesService.addtreeshouquan(json));
    }
    @PostMapping("/testguajie")
    public void  testguajie() {
        //364个文件
        int filenum=364;
        File parentFile = new File("F://sdguajie");
        File[] archiveFile = parentFile.listFiles();
        Date date=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String rq=sdf.format(date);
        for (int i = 0; i < filenum; i++) {
            String sql=" select id from( select rownum as nowrow,id from BZBDAS_01_KJJJ_1_F_A d where is_del=0 and state =5 ) where nowrow%"+filenum+"="+i;
            List<Map<String,Object>> dalist=yqdaTreesService.testsql(sql);
            StringBuffer sb=new StringBuffer();
            for (int j = 0; j < dalist.size(); j++) {
                sb.append("insert into BZBDAS_01_KJJJ_1_E_A(DOC_ID,CON_TABLE_ID,PATH,DOC_ORDER,DOC_NAME,ACTION_TIME,DOC_CREATE_TIME,TO_OFD,DOC_FILE_TYPE,CREATE_BY,IS_DEL,CREATE_TIME,DOC_FILE_SIZE,IS_INDEX,TYPE,REAL_DOC_NAME,DOC_MNT)values(");
                sb.append(SnowflakeIdGenerator.generateId()+","+dalist.get(j).get("ID")+","+"'sdguajie/"+archiveFile[i].getName()+"',1,'"+archiveFile[i].getName()+"','"+rq+"','"+rq+"',0,'PDF','dagly',0,'"+rq+"','"+archiveFile[i].length()+"',0,1,'"+archiveFile[i].getName()+"','/opt/mnt/');");
            }
            System.out.println("插入文档表sql===="+sb);
            yqdaTreesService.insertsql(sb.toString());
        }
    }
    //
    @Log(title = "根据配置的授权获取档案树", businessType = BusinessType.SELECT)
    @GetMapping("/getNewtrees/{menuId}")
    public AjaxResult getNewtrees(@PathVariable Long menuId){
        Boolean isDagly = SecurityUtils.isDagly();
        if(isDagly){
            return success(yqdaTreesService.getNewTreeList());
        }
        return success(yqdaTreesService.getNewtrees(menuId));
    }
}
